package com.hisan.freeride.common.view;

import android.app.Activity;
import android.app.Dialog;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.hisan.freeride.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimePickUtils implements OnValueChangeListener,
		OnClickListener {
	private String initDateTime;
	private Activity activity;
	private Calendar calendar;
	private CustomNumberPicker hourpicker;//小时
	private CustomNumberPicker minutepicker;//分钟
	private CustomNumberPicker datepicker;//日期
	private String[] minuteArrs;
	private String hourStr;//小时
	private String minuteStr;//分钟
	private String dateStr;
	private Dialog dialog;
	private TextView startDateTime;
	private TextView tv_time;
	private String[] dayArrays;
	private long currentTimeMillis;
	//根据日期来更换时间选择的状态
	private  String tempDate = "";
	private  int hours, minutes;

	public DateTimePickUtils(Activity activity, String initDateTime,
			TextView startDateTime) {
		this.startDateTime = startDateTime;
		this.activity = activity;
		this.initDateTime = initDateTime;

	}

	public void initPicker() {
		Calendar calendar = Calendar.getInstance();
		//时
		 hours = calendar.get(Calendar.HOUR_OF_DAY);
		//分钟
		 minutes = calendar.get(Calendar.MINUTE);
		//当前月份
//		int month = calendar.get(Calendar.MONTH) + 1;
		int month = 6;
//		int day = calendar.get(Calendar.DAY_OF_MONTH);
//		LogUtils.v("xxx",day);
		if (55 <= minutes)
			minutes = 0;
		else if (50 <= minutes)
			minutes = 11;
		else if (45 <= minutes)
			minutes = 10;
		else if (40 <= minutes)
			minutes = 9;
		else if (35 <= minutes)
			minutes = 8;
		else if (30 <= minutes)
			minutes = 7;
		else if (25 <= minutes)
			minutes = 6;
		else if (20 <= minutes)
			minutes = 5;
		else if (15 <= minutes)
			minutes = 4;
		else if (10 <= minutes)
			minutes = 3;
		else if (05 <= minutes)
			minutes = 2;
		else if (00 <= minutes){
			minutes = 1;
		}

		// 设置日期 7天以内
		dayArrays = new String[30];
		for (int i = 0; i < 30; i++) {
			 tempDate = "";
			currentTimeMillis = System.currentTimeMillis() + i * 24 * 3600
					* 1000;
			calendar.setTime(new Date(currentTimeMillis));
			switch (i) {
				case 0:
					tempDate = "今天";
					break;
				case 1:
					tempDate = "明天";
					break;
				case 2:
					tempDate = "后天";
					break;
				default:
					//奇月
				if ( month % 2 != 0){
				int MONTH=calendar.get(Calendar.DAY_OF_MONTH) +1;
					if (month==2){
						if (MONTH>=28){
							MONTH=28;
						}
					}else{
						if (MONTH>=30){
							MONTH=30;
						}
					}
					tempDate = calendar.get(Calendar.MONTH) + 1 + "月" + MONTH+ "日";
					//偶月
				}else{
					int MONTH=calendar.get(Calendar.DAY_OF_MONTH) +1;
					if (MONTH>=31){
						MONTH=31;
						tempDate = calendar.get(Calendar.MONTH) + 1 + "月" + MONTH+ "日";
					}else{
						tempDate = calendar.get(Calendar.MONTH) + 1 + "月" + MONTH+ "日";
					}
				}
					break;
			}
			dayArrays[i] = tempDate;
		}
		currentTimeMillis = System.currentTimeMillis();// 设置当前时间的毫秒值
		datepicker.setOnValueChangedListener(this);
		datepicker.setDisplayedValues(dayArrays);
		datepicker.setMinValue(0);
		datepicker.setMaxValue(dayArrays.length - 1);
		datepicker.setValue(0);
		dateStr = dayArrays[0];// 初始值
		// 设置小时 预约30分钟以后
		hourpicker.setOnValueChangedListener(this);
		if (minutes == 0) {
			hourpicker.setValue(hours + 1);
			hourStr = hours + 1 + "";// 初始值
		} else {
			hourpicker.setValue(hours);
			hourStr = hours + "";// 初始值
		}
		//设置最大时间
		hourpicker.setMaxValue(23);
		//设置最小时间
		hourpicker.setMinValue(hours);
		//设置系统当前时间
		hourpicker.setValue(hours);
		// 设置分钟
		minuteArrs = new String[] { "00", "05", "10", "15", "20", "25","30","35","40","45",
				"50","55"};
		minutepicker.setOnValueChangedListener(this);
		minutepicker.setDisplayedValues(minuteArrs);
		minutepicker.setMinValue(0);
		minutepicker.setMaxValue(minuteArrs.length - 1);
		minutepicker.setValue(minutes);
		minuteStr = minuteArrs[minutes];// 初始值

	}

	/**
	 * 弹出日期时间选择框方法
	 * 
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public Dialog dateTimePicKDialog() {
		View dateTimeLayout = View.inflate(activity, R.layout.common_datetime2,
				null);
		dateTimeLayout.setMinimumWidth(10000);
		dateTimeLayout.findViewById(R.id.tv_cancel).setOnClickListener(this);
		dateTimeLayout.findViewById(R.id.tv_confirm).setOnClickListener(this);
		tv_time = (TextView) dateTimeLayout.findViewById(R.id.tv_time);
		datepicker = (CustomNumberPicker) dateTimeLayout
				.findViewById(R.id.datepicker);
		hourpicker = (CustomNumberPicker) dateTimeLayout
				.findViewById(R.id.hourpicker);
		minutepicker = (CustomNumberPicker) dateTimeLayout
				.findViewById(R.id.minuteicker);
		datepicker
				.setDescendantFocusability(CustomNumberPicker.FOCUS_BLOCK_DESCENDANTS);
		hourpicker
				.setDescendantFocusability(CustomNumberPicker.FOCUS_BLOCK_DESCENDANTS);
		minutepicker
				.setDescendantFocusability(CustomNumberPicker.FOCUS_BLOCK_DESCENDANTS);
		initPicker();
		dialog = new Dialog(activity);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(dateTimeLayout);

		Window window = dialog.getWindow();

		window.setWindowAnimations(R.style.main_menu_animstyle);
		WindowManager.LayoutParams wl = window.getAttributes();
		wl.x = 0;
		wl.y = (activity).getWindowManager().getDefaultDisplay()
				.getHeight();
		wl.width = LayoutParams.MATCH_PARENT;
		wl.height = LayoutParams.WRAP_CONTENT;
		dialog.onWindowAttributesChanged(wl);
		// 设置点击外围隐藏
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		WindowManager windowManager = activity.getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
		lp.width = display.getWidth(); //设置宽度
		dialog.getWindow().setAttributes(lp);
		onDateChanged();
		return dialog;
	}

	@SuppressWarnings("deprecation")
	public void onDateChanged() {
		// 获得日历实例
		calendar = Calendar.getInstance();
		calendar.setTime(new Date(currentTimeMillis));
		Date date = calendar.getTime();
		date.setHours(Integer.parseInt(hourStr));
		date.setMinutes(Integer.parseInt(minuteStr));
		calendar.setTime(date);
		//选择其他日期中的参数
		if(datepicker.getValue()>0)	{
			hourpicker.setMaxValue(23);
			//设置最小时间
			hourpicker.setMinValue(0);
			minutepicker.setMinValue(0);
			minutepicker.setValue(minutes);
			//当前日期
		}else{
			hourpicker.setMaxValue(23);
			//设置最小时间
			hourpicker.setMinValue(hours);
			minutepicker.setMinValue(0);
			minutepicker.setValue(minutes);
		}
		if (calendar.getTimeInMillis() <= System.currentTimeMillis() + 1800000) {
			tv_time.setText("请发布30分钟后的有效行程");
		} else {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			initDateTime = sdf.format(calendar.getTime()) + " " + hourStr + ":"
					+ minuteStr;
			tv_time.setText(initDateTime);
		}
	}


	@Override
	public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
		switch (picker.getId()) {
		case R.id.datepicker:
			currentTimeMillis = System.currentTimeMillis() + newVal * 24 * 3600
					* 1000;
			dateStr = dayArrays[newVal];
			onDateChanged();
			break;
		case R.id.hourpicker:
			hourStr = newVal + "";
			onDateChanged();
			break;
		case R.id.minuteicker:
			minuteStr = minuteArrs[newVal];
			onDateChanged();
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_cancel:
			dialog.dismiss();
			break;
		case R.id.tv_confirm:
			if (calendar.getTimeInMillis() <= System.currentTimeMillis() + 1800000) {
				Toast.makeText(activity, "请发布当前时间30分钟后的有效行程", Toast.LENGTH_SHORT)
						.show();
			} else {
				startDateTime.setText(initDateTime);
				dialog.dismiss();
			}

			break;
		default:
			break;
		}
	}

}
